import 'package:f_enjoy_plus_community/pages/detail/index.dart';
import 'package:f_enjoy_plus_community/pages/house/building_list.dart';
import 'package:f_enjoy_plus_community/pages/house/house_detail.dart';
import 'package:f_enjoy_plus_community/pages/house/house_list.dart';
import 'package:f_enjoy_plus_community/pages/house/location_list.dart';
import 'package:f_enjoy_plus_community/pages/house/room_list.dart';
import 'package:f_enjoy_plus_community/pages/login/index.dart';
import 'package:f_enjoy_plus_community/pages/profile/index.dart';
import 'package:f_enjoy_plus_community/pages/tab_bar/index.dart';
import 'package:f_enjoy_plus_community/utils/token.dart';
import 'package:flutter/material.dart';

void main() {
  runApp(
    MaterialApp(
      routes: {
        '/': (context) => const TabBarPage(),
        '/detail': (context) => const NoticeDetail(),
        '/login': (context) => const LoginPage(),
      },
      //获取用户要跳转的路径如果routes中没有则就要走onGenerateRoute方法
      //如果有token直接跳转
      //如果没有token跳转登录页面
      onGenerateRoute: (settings){
        //这个方法不支持异步操作
        final tokenManager = TokenManager();
        final token = tokenManager.getToken() ?? '';
        if(token.isEmpty && settings.name !='/login'){
          return MaterialPageRoute(builder: (context) => const LoginPage());
        }

        if(settings.name == '/profile'){
          return MaterialPageRoute(builder: (context) =>  ProfilePage(userInfo: settings.arguments as Map,));
        }

        //跳转到房屋列表
        if(settings.name == '/house_list'){
          return MaterialPageRoute(builder: (context) =>  HouseList());
        }

        //去我的房屋列表
        if(settings.name == '/location_list'){
          return MaterialPageRoute(builder: (context) =>  LocationList());
        }

        // 去我的房屋列表
        if (settings.name == '/house_list') {
          return MaterialPageRoute(builder: (context) =>  HouseList());
        }

        // 单元列表
        if (settings.name == '/location_list') {
          return MaterialPageRoute(builder: (context) => const LocationList());
        }

        // 楼栋列表
        if (settings.name == '/building_list') {
          return MaterialPageRoute(
              builder: (context) =>
                  BuildingList(point: (settings.arguments as Map)['point']));
        }

        // 房间列表
        if (settings.name == '/room_list') {
          Map temp = settings.arguments as Map;
          return MaterialPageRoute(
              builder: (context) =>
                  RoomList(point: temp['point'], building: temp['building']));
        }

        // // 添加房屋
        // if (settings.name == '/house_form') {
        //   return MaterialPageRoute(
        //       builder: (context) =>
        //           HouseForm(houseInfo: settings.arguments as Map));
        // }

        // 添加房屋
        if (settings.name == '/house_detail') {
          return MaterialPageRoute(
              builder: (context) =>
                  HouseDetail(houseInfo: settings.arguments as Map));
        }


        //404页面,没有匹配到路径就跳转主页面
        return MaterialPageRoute(builder: (context) =>  TabBarPage());
      },

      //设置默认索引为'/',也就是TabBarPage()
      initialRoute: '/',
    ),
  );
}
